package mission

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sanguo/node/common/actionEvent"

	"github.com/sniperHW/kendynet"
)

type BaseInfo struct {
	XMLName       xml.Name `xml:"base_info"`
	Id            int32    `xml:"id"`
	Type          int32    `xml:"type"`
	StoryStage    int32    `xml:"story_stage"`
	NextMissionID int32    `xml:"next_mission_id"`
	AutoAccept    bool     `xml:"auto_accept"`
	AutoComplete  bool     `xml:"auto_complete"`
	DayLimit      int32    `xml:"day_limit"`
}

type Condition struct {
	XMLName xml.Name `xml:"condition"`
	Id      int32    `xml:"condition_id"`
	Type    int32    `xml:"condition_type"`
	Need    int32    `xml:"condition_need"`
}

type ConditionList struct {
	XMLName    xml.Name    `xml:"condition_list"`
	Conditions []Condition `xml:"condition"`
}

type CompleteInfo struct {
	XMLName         xml.Name      `xml:"complete_info"`
	ConditionList   ConditionList `xml:"condition_list"`
	SuccessfulEvent string        `xml:"successful_event"`
	ActionEvent     []actionEvent.Action
}

type DeliverInfo struct {
	XMLName               xml.Name `xml:"deliver_info"`
	DeliverCondition      string   `xml:"deliver_condition"`
	DeliverConditionEvent []actionEvent.Action
	DeliverEvent          string `xml:"deliver_event"`
	ActionEvent           []actionEvent.Action
}

type Item struct {
	XMLName xml.Name `xml:"item"`
	Id      int32    `xml:"item_id"`
	Count   int32    `xml:"item_count"`
}

/*type AwardPreviewList struct {
	XMLName xml.Name `xml:"award_preview_list"`
	Items   []Item   `xml:"item"`
}*/

/*type Award struct {
	XMLName          xml.Name         `xml:"award"`
	AwardEvent       string			  `xml:"award_event"`
	ActionEvent 	 []actionEvent.Action
}*/

type Mission struct {
	XMLName      xml.Name     `xml:"mission"`
	BaseInfo     BaseInfo     `xml:"base_info"`
	CompleteInfo CompleteInfo `xml:"complete_info"`
	DeliverInfo  DeliverInfo  `xml:"deliver_info"`
	//Award        Award        `xml:"award"`
}

var missionDefs map[int32]Mission

func init() {
	missionDefs = map[int32]Mission{}
}

func GetDef(missionID int32) *Mission {
	if m, ok := missionDefs[missionID]; ok {
		return &m
	} else {
		return nil
	}
}

func getFilelist(path string) bool {
	err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
		if f != nil && !f.IsDir() {
			s := len(path)
			//fmt.Println(s)
			if s > 4 && ".xml" == path[s-4:] {
				content, e := ioutil.ReadFile(path)
				if e != nil {
					kendynet.Errorf("load %s error:%s\n", path, e.Error())
					return e
				}
				var mission Mission
				e = xml.Unmarshal(([]byte)(content), &mission)
				if e != nil {
					kendynet.Errorf("load %s error:%s\n", path, e.Error())
					return e
				}
				//mission.Award.AwardEvent = "GetItem(item:30003,dada:231;item:30001)#GetItem1(item:30004,dada:232;item:30002)"
				mission.CompleteInfo.ActionEvent = actionEvent.Analysis(mission.CompleteInfo.SuccessfulEvent)
				mission.DeliverInfo.DeliverConditionEvent = actionEvent.Analysis(mission.DeliverInfo.DeliverCondition)
				mission.DeliverInfo.ActionEvent = actionEvent.Analysis(mission.DeliverInfo.DeliverEvent)
				missionDefs[mission.BaseInfo.Id] = mission
			}
		}
		return nil
	})
	if err != nil {
		return false
	} else {
		return true
	}
}

func Load(path string) bool {
	return getFilelist(path)
}

func test() error {
	str :=
		`
<?xml version="1.0" encoding="utf-8"?>
<mission>
  <base_info>
    <id>1000</id>
    <type>1</type>
    <next_mission_id>1001</next_mission_id>
    <auto_accept>false</auto_accept>
    <auto_complete>false</auto_complete>
    <day_limit>1</day_limit>
    <refresh_time>
      <weekday>1</weekday>
      <hour>12</hour>
      <minute>10</minute>
      <second>10</second>
    </refresh_time>
    <title>击败敌军</title>
    <description>前面发现了贪狼军，击溃他们吧</description>
    <goal>击败前方的贪狼军</goal>
  </base_info>

  <obtain_info>
    <obtain_npc_id>2000</obtain_npc_id>
    <node_info>
      <node_type>0</node_type>
      <node_id>1234</node_id>
      <node_trigger_event></node_trigger_event>
    </node_info>
    <mission_buff>201</mission_buff>
    <obtain_dialogue_id>301</obtain_dialogue_id>
    <obtain_event></obtain_event>
    <obtain_repeat_event></obtain_repeat_event>
  </obtain_info>

  <complete_info>
    <condition_list>
      <condition>
        <condition_type>1</condition_type>
        <condition_id>10231</condition_id>
        <condition_need>10</condition_need>
        <node_info>
          <node_type>1</node_type>
          <node_id>2314</node_id>
          <node_trigger_event></node_trigger_event>
        </node_info>
      </condition>
      <condition>
        <condition_type>1</condition_type>
        <condition_id>10232</condition_id>
        <condition_need>10</condition_need>
        <node_info>
          <node_type>1</node_type>
          <node_id>2314</node_id>
          <node_trigger_event></node_trigger_event>
        </node_info>
      </condition>

    </condition_list>
    <successful_event></successful_event>
    <failed_event></failed_event>
  </complete_info>

  <deliver_info>
    <deliver_condition></deliver_condition>
    <deliver_npc_id>1003</deliver_npc_id>
    <node_info>
      <node_type>0</node_type>
      <node_id>1242</node_id>
      <node_trigger_event></node_trigger_event>
    </node_info>
    <uncomplete_dialogue_id>5222</uncomplete_dialogue_id>
    <deliver_dialogue_id>5223</deliver_dialogue_id>
    <deliver_event></deliver_event>
  </deliver_info>

  <award>
    <award_event>GetItem(item:30003,dada:231;item:30001)#GetItem1(item:30004,dada:232;item:30002)</award_event>
    <award_preview_list>
      <item>
        <item_id>1234</item_id>
        <item_count>10</item_count>
      </item>
      <item>
        <item_id>1235</item_id>
        <item_count>1</item_count>
      </item>
    </award_preview_list>
  </award>
</mission>
`
	var mission Mission
	err := xml.Unmarshal(([]byte)(str), &mission)

	if nil != err {
		fmt.Println(err)
	} else {
		//fmt.Println(mission)
	}
	return nil
}

func GetItem(a ...interface{}) {
	fmt.Println(a)
}

func GetItem1(a ...interface{}) {
	fmt.Println(a)
}
